home *** CD-ROM | disk | FTP | other *** search
- unit MemoEgU;
-
- interface
-
- uses
- WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
-
- type
- TForm1 = class(TForm)
- Memo1: TMemo;
- procedure Memo1Change(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- type
- TWCAccess = class(TWinControl);
-
- function TextHeight(Ctrl: TWinControl; const Msg: String): Integer;
- var
- DC: HDC;
- OldFont: HFont;
- Size: TSize;
- begin
- { Can't just ask a control for the font height, as Delphi }
- { caches the font and doesn't select it into the device }
- { context until some drawing is required. }
- { The memo may have a different font to its form and under }
- { those circumstances, you could get bad results. }
-
- { Access control's device context }
- DC := GetDC(Ctrl.Handle);
- try
- { Ensure font is selected into DC (saving old font) }
- OldFont := SelectObject(DC, TWCAccess(Ctrl).Font.Handle);
- try
- { Find text height }
- {$ifdef Win32}
- Win32Check(GetTextExtentPoint32(DC, PChar(Msg), 1, Size));
- {$else}
- GetTextExtentPoint(DC, @(Msg[1]), 1, Size);
- {$endif}
- Result := Size.cy
- finally
- { Put old font back into memo }
- SelectObject(DC, OldFont)
- end;
- finally
- { Let the DC go }
- ReleaseDC(Ctrl.Handle, DC)
- end;
- end;
-
- procedure TForm1.Memo1Change(Sender: TObject);
- var
- Memo: TMemo;
- MemoNumLines: Integer;
- OldSelStart, OldSelLength: Integer;
- begin
- if Sender is TMemo then
- Memo := TMemo(Sender)
- else
- Exit;
- MemoNumLines := Memo.ClientHeight div TextHeight(Memo, 'X');
- { Record where we were }
- OldSelStart := Memo.SelStart;
- OldSelLength := Memo.SelLength;
- { Would use the Count property of Lines, but }
- { this doesn't count a blank line at the end }
- { if Memo.Lines.Count > MemoNumLines then }
- if Memo.Perform(EM_GETLINECOUNT, 0, 0) > MemoNumLines then
- Memo.ScrollBars := ssVertical
- else
- Memo.ScrollBars := ssNone;
- { Go back to old position after memo control (possibly) recreated }
- Memo.SelStart := OldSelStart;
- Memo.SelLength := OldSelLength;
- end;
-
- end.
-